//
//  PhotoGridViewcontroller.swift
//  PhotoBrowse
//
//  Created by baiju on 2023/11/26.
//  Copyright © 2023 Apple. All rights reserved.
//

import UIKit
import Photos

// 定义section和item类型
enum Section {
    case main
}

struct Item: Hashable {
    let id = UUID()
    let title: String
}

class PhotoGridViewController: UITableViewController {

    // 定义Diffable Data Source
    private var dataSource: UITableViewDiffableDataSource<Section, Item>!

    override func viewDidLoad() {
        super.viewDidLoad()

        // 初始化Diffable Data Source
        dataSource = UITableViewDiffableDataSource<Section, Item>(tableView: tableView) {
            (tableView: UITableView, indexPath: IndexPath, item: Item) -> UITableViewCell? in
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = item.title
            return cell
        }

        // 初始数据
        var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
        snapshot.appendSections([.main])
        let items = [Item(title: "First"), Item(title: "Second")]
        snapshot.appendItems(items)
        dataSource.apply(snapshot, animatingDifferences: true)
    }
}
